package ldh.fx.adminfx.ui;

import javafx.geometry.Pos;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.*;
import org.kordamp.ikonli.fontawesome.FontAwesome;
import org.kordamp.ikonli.javafx.FontIcon;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class IndexPane extends VBox {

    public IndexPane() {
//        this.getStyleClass().add("index-pane");
        initHeader();
        initChart();
    }

    private void initChart() {
        GridPane gridPane = new GridPane();
        gridPane.getStyleClass().add("chart-pane");

        VBox vBox1 = new VBox();
        GridPane.setConstraints(vBox1, 0, 0, 2, 1);

        StackPane stackPane = new StackPane();
        stackPane.getStyleClass().add("block-pane");
        NumberAxis xaxis = new NumberAxis();
        NumberAxis yaxis = new NumberAxis();
        LineChart<Number, Number> xyChart = new LineChart(xaxis, yaxis);
        buildXyChartData(xyChart);
        stackPane.getChildren().add(xyChart);
        stackPane.setPrefHeight(400);
        gridPane.getChildren().add(stackPane);

        StackPane stackPane2 = new StackPane();
        stackPane2.getStyleClass().add("block-pane");
        stackPane2.getChildren().add(new Label("2"));
        stackPane2.setPrefHeight(200);

        gridPane.getChildren().add(stackPane2);
        vBox1.getChildren().addAll(stackPane, stackPane2);


        GridPane gridPane1 = new GridPane();
        GridPane.setConstraints(gridPane1, 2, 0, 2, 1);
        StackPane stackPane3 = new StackPane();
        stackPane3.getStyleClass().add("block-pane");
        stackPane3.getChildren().add(new Label("3"));
        stackPane3.setPrefHeight(50);
        GridPane.setConstraints(stackPane3, 0, 0);

        StackPane stackPane4 = new StackPane();
        stackPane4.getStyleClass().add("block-pane");
        stackPane4.getChildren().add(new Label("4"));
        stackPane4.setPrefHeight(50);
        GridPane.setConstraints(stackPane4, 1, 0);

        StackPane stackPane5 = new StackPane();
        stackPane5.getStyleClass().add("block-pane");
        stackPane5.getChildren().add(new Label("5"));
        stackPane5.setPrefHeight(100);
        GridPane.setConstraints(stackPane5, 0, 1, 2, 1);

        StackPane stackPane6 = new StackPane();
        stackPane6.getStyleClass().add("block-pane");
        stackPane6.getChildren().add(new Label("6"));
        stackPane6.setPrefHeight(100);
        GridPane.setConstraints(stackPane6, 0, 2, 2, 1);

        ColumnConstraints cc1 = new ColumnConstraints();
        cc1.setHgrow(Priority.ALWAYS);
        ColumnConstraints cc2 = new ColumnConstraints();
        cc2.setHgrow(Priority.ALWAYS);
        gridPane1.getColumnConstraints().addAll(cc1, cc2);
        gridPane1.getChildren().addAll(stackPane3, stackPane4, stackPane5, stackPane6);

        ColumnConstraints c1 = new ColumnConstraints();
        c1.setHgrow(Priority.ALWAYS);
        ColumnConstraints c2 = new ColumnConstraints();
        c2.setHgrow(Priority.ALWAYS);
        ColumnConstraints c3 = new ColumnConstraints();
        c3.setHgrow(Priority.ALWAYS);
        ColumnConstraints c4 = new ColumnConstraints();
        c4.setHgrow(Priority.ALWAYS);
        gridPane.getColumnConstraints().addAll(c1, c2, c3, c4);
        gridPane.getChildren().addAll(vBox1, gridPane1);

        this.getChildren().add(gridPane);
    }

    private void initHeader() {
        HBox hBox = new HBox();
        hBox.setAlignment(Pos.CENTER_LEFT);
        hBox.getStyleClass().add("h-pane");

        VBox vBox = new VBox();
        vBox.setSpacing(5);
        Label titleLabel = new Label("Dashboard");
        titleLabel.getStyleClass().add("h3");
        FontIcon fontIcon0 = FontIcon.of(FontAwesome.DASHBOARD);
        titleLabel.setGraphic(fontIcon0);

        Label descLabel = new Label("Description");
        descLabel.getStyleClass().add("h5");
        vBox.getChildren().addAll(titleLabel, descLabel);

        Region region = new Region();
        HBox.setHgrow(region, Priority.ALWAYS);
        Button button = new Button("首页");
        FontIcon fontIcon = FontIcon.of(FontAwesome.DASHBOARD);
        button.setGraphic(fontIcon);
        hBox.getChildren().addAll(vBox, region, button);
        this.getChildren().add(hBox);
    }

    private void buildXyChartData(LineChart<Number, Number> xyChart) {
        Random random = new Random();
        int num = randomInt(random, 1,5);
        for(int i=0; i<num; i++) {
            int n = randomInt(random, 10,50);
            XYChart.Series<Number, Number> series = new XYChart.Series<>();
            List<XYChart.Data<Number, Number>> dataList = new ArrayList();
            int t = random.nextInt(100);
            int r = randomInt(random, 10, 50);
            for(int j=0; j<n; j++) {
                int x = t + (int)(r * j + 10);
                double y = random.nextDouble() * t;
                dataList.add(new XYChart.Data<>(x, y));
            }
            dataList.sort((o1, o2)->{
                return o1.getXValue().intValue() -  o2.getXValue().intValue();
            });
            series.getData().addAll(dataList);
            series.setName("demo" + i);
            xyChart.getData().add(series);
        }
    }

    private int randomInt(Random random, int minValue, int num) {
        int t = random.nextInt(num);
        while(t < minValue) {
            t = random.nextInt(num);
        }
        return t;
    }
}
